package com.uff.hmstpa.model.persistence.impl;

import com.uff.hmstpa.model.domain.valueobject.Patient;
import com.uff.hmstpa.model.persistence.AbstractModelDAO;
import com.uff.hmstpa.model.persistence.PatientDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

public class PatientDAOImpl extends AbstractModelDAO implements PatientDAO {

    private static PatientDAOImpl SOLE_INSTANCE;

    private PatientDAOImpl() {
        super();
    }

    public static PatientDAO getInstance() {
        if (SOLE_INSTANCE == null) {
            SOLE_INSTANCE = new PatientDAOImpl();
        }
        return SOLE_INSTANCE;
    }

    public void persist(String cpf, String name) {
        //TODO persiste paciente
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Patient> loadPatients(String doctorCRM) {
        //TODO carrega lista de pacientes de um doutor
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(String cpf) {
        //TODO deleta paciente
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Patient retrieve(String cpf) {
        return jdbcTemplate.queryForObject("SELECT * FROM patient WHERE cpf = ?", new Object[]{cpf}, new PatientMapper());
    }
    
    private static final class PatientMapper implements RowMapper<Patient> {

        public Patient mapRow(ResultSet rs, int rowNum) throws SQLException {
            Patient patient = new Patient();
            patient.setCpf(rs.getString("cpf"));
            patient.setName(rs.getString("name"));
            return patient;
        }
    }
}
